// 回到顶部
$(".header-content").css({
      display : "none",
  })
  //   页面跳转到活动页
  $(".ads_image").on("click" , function(){
    location.href ="./activity.html"
  })
  // 删除活动跳转
  $("html").on("click" ,function(){
    $(".activity-page").remove();
  })
  window.addEventListener( "load" , ()=>{
    class EventList{
        constructor(){
            // 单写一个初始化
            this.init();
        }
        init(){
            // 初始化基础元素
            this.login_btn  = document.getElementById("loginBtn");
            console.log(this.login_btn);
            // 初始化用户名元素
            this.nickname = document.getElementById("nickname");
            // 获取cookie中的数据 ID , TOKEN
            this.ID = cookie("ID");
            this.TOKEN = cookie("Token");
  
            console.log(this.ID , this.TOKEN );
            // 做出对应的判断length大于0证明有值
            if(this.ID.length > 0 && this.TOKEN.length > 0){
                this.getUserInfo();
                this.login_btn.style.display  = "none";
            }else{
                 // 没有登录信息，展示登录按钮; 
                this.nickname.style.display = "none";
            }
        }
        // 获取免登陆信息
         getUserInfo(){
            // 传参
            let options = {
                url : "http://localhost:8888/users/info/" + this.ID,
                headers : {
                    "authorization" : this.TOKEN
                },
                dataType : "json",
            }
           axios(options).then(function(response){
                // console.log(response);
                // console.log(response.data);
              let res = response.data;
              console.log(res.info.nickname);
              $("#nickname").text(res.info.nickname);
           })
        }
    }
   new   EventList;
  })
  
  
  // 
  class GoodList{
      constructor(){
            this.init();
      }
      // async是对promise的一次更改
      async init(){
            
            this.goods_list = document.querySelector(".group-ul");
            this.data = null;
            this.current = 0;
            this.num = 0;
          //   this.pagesize = 30;
          
            this.bindEvent();
            try{
             
            let res = await this.loadData();
          //   console.log(res);
            this.data = res;
          //   console.log(this.data);
            this.render();
            this.initPagination();
  
           
           
  
            }catch(e){
                  // location.href = "login.html"
            }
      }
      bindEvent(){
            on(this.goods_list , "click" , ".goods" , (e)=>{
                  this.go(e.origin.getAttribute("data-id"));
                  console.log(e.origin.getAttribute("data-id"))
            });
      }
      
     loadData(){
          //   let source = axios.CancelToken.source();
                  let options = {
                      url : "../static/data/list.json",
                      //   cancelToken: source.token,
                      //   source : source,
                  };
                  return  $.ajax(options);
      }
      render(){
            var html = "";
            for(var i = this.current * this.num ; i < this.current * this.num + this.num; i ++  ){
                  html +=`<li>
                                  <div class="goods"  data-id="${this.data[i].bookid}">
                                    <div class="cell-books">
                                    <div class="img">
                                    <img data-src="${this.data[i].src}">
                                    </div>
                                    <div class="price">
                                    <span class="price-n">
                                    <span>￥</span>
                                          ${this.data[i].price}
                                    <span>
                                    <span class="price-o">${this.data[i].oldprice}</span>
                                    </div>
                                    <div class="name">
                                          <a>${this.data[i].title}</a>
                                    </div>
                                    <div class="publisher">
                                          <a>长江出版社</a>
                                    </div>
                                    <div class="opt-btns">
                                    <div class="cart" data-id="${this.data[i].bookid}">
                                          加入购物车
                                    </div>
                                    <div class="collection">
                                          收藏
                                    </div>
                                    </div>
                                    </div>
                                    </div>
                                    </li>`
            $(".group-ul").html(html);
            }
      }
      go(id){
            location.href = "detail.html#id=" + id;
           
      }
      initPagination(){
            new Pagination(this.data.length,{
                  pagination : ".sg-commentpage",
                  callback : async (index , num) => {
        
  
                        this.current = index; 
                      //   console.log(this.current);
                        this.num =  num;
                      //   console.log(num);
                        let res =  await this.loadData();
                      //   console.log(res);
                        this.data = res;
                        this.render();  
                      //   console.log(this.data); 
                      new LazyLoad(".img img");  
                  }
            })
      } 
  }
  new GoodList;
  
  
  
  